package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.count.QueryCountRequest;
import com.yupi.springbootinit.model.dto.count.RechargeRequest;
import com.yupi.springbootinit.model.dto.count.VisitData;
import com.yupi.springbootinit.model.entity.CallAndGenerateCounts;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.vo.CallAndGenerateCountsVO;
import com.yupi.springbootinit.model.vo.GenHistoryVO;
import com.yupi.springbootinit.service.CallAndGenerateCountsService;
import com.yupi.springbootinit.service.GenHistoryService;
import com.yupi.springbootinit.service.UserService;
import com.yupi.springbootinit.utils.ResMockData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/callAndGen")
@Slf4j
public class countController {

    @Resource
    private CallAndGenerateCountsService countsService;

    @Resource
    private UserService userService;

    @Resource
    private GenHistoryService genHistoryService;


    /**
     * 根据当前用户的id查询统计表
     */
    @PostMapping("/query")
    public BaseResponse<CallAndGenerateCountsVO> listMyChartPage(@RequestBody @Validated QueryCountRequest queryCountRequest,
                                                         HttpServletRequest request) {
        userService.getLoginUser(request,request.getHeader("token"));
        CallAndGenerateCountsVO callAndGenerateCountsVO = countsService.queryCount(queryCountRequest);
        return ResultUtils.success(callAndGenerateCountsVO);
    }

    /**
     * 查询历史调用
     */
    @PostMapping("/queryGenHistoryData")
    public BaseResponse<List<GenHistoryVO>> queryGenHistoryData(HttpServletRequest request) {
        User user = userService.getLoginUser(request, request.getHeader("token"));
        return ResultUtils.success(genHistoryService.queryGenHistoryData(user.getId()));
    }

    /**
     * 充值调用次数与生成次数
     */
    @PostMapping("/rechargeCount")
    public BaseResponse<Boolean> rechargeCount(@RequestBody RechargeRequest rechargeRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request, request.getHeader("token"));
        int count = rechargeRequest.getMoney();
        int useMoney = count*10;
        CallAndGenerateCounts generateCounts = countsService.getByUserId(user.getId());
        //当前用户余额
        Integer currentBalance = generateCounts.getBalance();
        if (useMoney>currentBalance){
            throw  new BusinessException(ErrorCode.FORBIDDEN_ERROR,"余额不足请充值！！");
        }
        generateCounts.setBalance(currentBalance-useMoney);
        Integer remainingCallCount = generateCounts.getRemainingCallCount();
        Integer remainingGenerateCount = generateCounts.getRemainingGenerateCount();
        remainingCallCount = remainingCallCount + count;
        remainingGenerateCount = remainingGenerateCount+count;
        generateCounts.setRemainingCallCount(remainingCallCount);
        generateCounts.setRemainingGenerateCount(remainingGenerateCount);
        return ResultUtils.success(countsService.updateById(generateCounts));
    }


    @PostMapping("/platformList")
    public HashMap<String, Object> PlatformList(HttpServletRequest request){
        userService.getLoginUser(request,request.getHeader("token"));
        //查询一个月以内,每日生成次数
        List<GenHistoryVO> genHistoryVOS = genHistoryService.queryGenHistoryData();
        //查询一个月以内,每日调用次数
        List<VisitData> visitDataList = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        HashMap<String, Object> resultMap = new HashMap<>();
        if (!genHistoryVOS.isEmpty()){
            genHistoryVOS.stream().forEach(genHistoryVO -> {
                Integer y = genHistoryVO.getCount();
                Date x = genHistoryVO.getDate();
                VisitData visitData = new VisitData();
                visitData.setX(x);
                visitData.setY(y);
                visitDataList.add(visitData);
            });
        }
        hashMap.put("visitData",visitDataList);
        hashMap.put("visitData2", ResMockData.getMap());
        hashMap.put("offlineData", ResMockData.getOffLineData());
        hashMap.put("salesData", ResMockData.getSalesData());
        hashMap.put("salesTypeData", ResMockData.getSalesTypeData());
        resultMap.put("data",hashMap);
        return resultMap;
    }




}
